import 'package:flutter/material.dart';
import 'package:flutter_basic/basic/basic_view.dart';
import 'package:flutter_basic/modules/mine/controller.dart';
import 'package:flutter_basic/route_manager.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MinePage extends BasicPage<MineController>{
  MinePage.bind(controller, {String? tag}) : super.bind(controller, tag: tag);

  final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text('我的'),
        centerTitle: true,
        actions: [
          IconButton(
              onPressed: (){
                Map<String, String> pageParam = Map();
                pageParam['title'] = '设置设置设置';
                Navigator.of(context).pushNamed(
                  RouteManager.settings,
                  arguments: pageParam,
                );
              },
              icon: Icon(Icons.settings)
          ),
        ],
      ),
      body: Obx((){
        return Center(
            child: Text(
            'Button tapped:${controller.number}'
            'This should persist across restarts.',
        ));
      }),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }

  Future<void> _incrementCounter() async {
    final SharedPreferences prefs = await _prefs;
    await prefs.reload();
    final int counter = (prefs.getInt('counter') ?? 0) + 1;
    await prefs.setInt('counter', counter);

    controller.number.value = counter;
  }
}